package com.efesco.inchar.list;

import java.util.Date;

import com.bstek.dorado.data.Dataset;
import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.data.Record;
import com.bstek.dorado.data.RecordIterator;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.utils.variant.VariantSet;
import com.bstek.dorado.view.DefaultViewModel;
import com.bstek.dorado.view.data.ViewDataset;


/**
 * ClassifyViewModel
 */
public class ClassifyViewModel extends DefaultViewModel {

  public void doClassify(ParameterSet parameters)
      throws Exception {
    Dataset datasetClassify = getDataset("datasetClassify");
    Dataset datasetDetail = getDataset("datasetDetail");
    long typeId = datasetDetail.getLong("IN_CHAR_EMPLOYEE_TYPE2_ID");
    String typeName = datasetDetail.getString("EMPLOYEE_TYPE_NAME");
    
    //RecordIterator iter = datasetClassify.recordIterator();
    datasetClassify.moveFirst();    
    while (!datasetClassify.isLast()) {
      Record record=datasetClassify.getCurrent();
      long inCharId = record.getLong("IN_CHAR_ID");
      String remark = record.getString("REMARK");
      addServiceTransaction(inCharId);     
      
      record.setString("EMPLOYEE_TYPE_NAME", typeName);
      record.setString("STATUS", "2");
      updateInCharList(inCharId, typeId, remark);
      datasetClassify.moveNext();
    }

    super.doUpdateData(parameters);
  }
  
  private void updateInCharList(long argInCharId, long argEmployeeType, String remark) throws Exception {
    DBStatement stmt = new DBStatement();
    try {
      stmt.setSql("UPDATE S_IN_CHAR_LIST SET EMPLOYEE_TYPE = " + argEmployeeType + 
      ", STATUS = '2',remark='"+remark+"' WHERE IN_CHAR_ID = " + argInCharId);
      stmt.execute();
      
      stmt.setSql("update s_in_char_list icl set icl.status='9' where icl.in_char_id="+argInCharId+" "
       +"and not exists(select 's' from s_in_char_service_transaction where icl.in_char_id=in_char_id and status=1)");
      stmt.execute();
    }
    finally {
      stmt.close();
    }

  }
  
  private void addServiceTransaction(long argInCharId) throws Exception {
    Dataset datasetTypeService = getDataset("datasetTypeService");
    
    //RecordIterator iter = datasetTypeService.recordIterator();
    datasetTypeService.moveFirst();
    while (!datasetTypeService.isLast()) {
      Record record = datasetTypeService.getCurrent();//iter.nextRecord();
      long serviceId = record.getLong("IN_CHAR_SERVICE_ID");
      String serviceType=record.getString("SERVICE_TYPE");
      insertServiceTransaction(argInCharId,serviceId,serviceType);
      addMaterialReceive(argInCharId);
      datasetTypeService.moveNext();
    }

  }
  
  private void insertServiceTransaction(long argInCharId, long argServiceId,String serviceType) throws Exception {
    DBStatement stmt = new DBStatement();
    try {
      String status="2";
      //33,1,10,30,20,11,13,12
      if (serviceType.equals("33")||serviceType.equals("1")||serviceType.equals("10")||serviceType.equals("30")||
    	  serviceType.equals("20")||serviceType.equals("11")||serviceType.equals("13")||serviceType.equals("12"))
      {
    	  status="1";
      }
      stmt.setSql("INSERT INTO S_IN_CHAR_SERVICE_TRANSACTION " + 
      "(IN_CHAR_SERVICE_TRANSACTION_ID, IN_CHAR_ID, IN_CHAR_SERVICE_ID, CREATE_DATE, UPDATE_DATE, STATUS) " +
      "VALUES (GENERAL.NEXTVAL," + argInCharId + "," + argServiceId + ",SYSDATE,SYSDATE,'"+status+"')");
      stmt.execute();
    }
    finally {
      stmt.close();
    }

  }
  
  private void addMaterialReceive(long argInCharId) throws Exception {
    Dataset datasetServiceMaterial = getDataset("datasetServiceMaterial");
    
    //RecordIterator iter = datasetServiceMaterial.recordIterator();
    datasetServiceMaterial.moveFirst();
    while (!datasetServiceMaterial.isLast()) {
      Record record = datasetServiceMaterial.getCurrent();// iter.nextRecord();
      long materialId = record.getLong("MATERIAL_ID");
      insertMaterialReceive(argInCharId, materialId);
      datasetServiceMaterial.moveNext();
    }

  }
  
  private void insertMaterialReceive(long argInCharId, long argMaterialId) throws Exception {
    DBStatement stmt = new DBStatement();
    try {
      stmt.setSql("INSERT INTO S_IN_CHAR_MATERIAL_RECEIVE " + 
      "(IN_CHAR_MATERIAL_RECEIVE_ID, IN_CHAR_ID, MATERIAL_ID, CREATE_DATE, UPDATE_DATE, STATUS) " +
      "VALUES (GENERAL.NEXTVAL, " + argInCharId + ", " + argMaterialId + ", SYSDATE, SYSDATE, '1')");
      stmt.execute();
    }
    finally {
      stmt.close();
    }

  }

}
